package com.jyw.activity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jyw.R;
import com.jyw.network.OkHttpManager;
import com.jyw.network.Server;
import com.jyw.pojo.OrderDetail;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @author simonw
 * @version 1.0
 * @created 2017/1/15
 */

public class OrderPaiedActivity extends BaseActivity implements View.OnClickListener {
    private TextView tvMoney, tvWl;
    private RecyclerView recycler;
    private YiAdapter adapter;
    private OrderDetail orderDetail;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_paied);
        tvMoney = (TextView) findViewById(R.id.tv_money);
        tvWl = (TextView) findViewById(R.id.tv_wl);
        recycler = (RecyclerView) findViewById(R.id.recycler);
        findViewById(R.id.iv_back).setOnClickListener(this);
        recycler.setLayoutManager(new LinearLayoutManager(OrderPaiedActivity.this));
        recycler.setAdapter(adapter = new YiAdapter());

        loadData();
    }

    class YiAdapter extends RecyclerView.Adapter<YiAdapter.YiHolder> {
        private List<OrderDetail.Yi> data = new ArrayList<>();

        void swapData(List<OrderDetail.Yi> data0) {
            this.data = data0;
            notifyDataSetChanged();
        }

        @Override
        public YiAdapter.YiHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new YiAdapter.YiHolder(LayoutInflater.from(OrderPaiedActivity.this).inflate(R.layout.item_yi, null));
        }

        @Override
        public void onBindViewHolder(YiAdapter.YiHolder holder, int position) {
            OrderDetail.Yi y = data.get(position);
            holder.tv.setText("衣服编号：" + y.getOrder_id() + "\n衣服名称：" + y.getName() +
                    "          衣服颜色：" + y.getColor() + "\n衣服品牌：" + y.getBrand() +
                    "          衣服数量：" + y.getNumber() + "\n衣服瑕疵：" + y.getFlaw());
        }

        @Override
        public int getItemCount() {
            return data == null ? 0 : data.size();
        }

        class YiHolder extends RecyclerView.ViewHolder {
            TextView tv;

            YiHolder(View itemView) {
                super(itemView);
                tv = (TextView) itemView.findViewById(R.id.tv);
            }
        }
    }

    private void loadData() {
        Request request = new Request.Builder().
                url(Server.GET_ORDER_DETAIL + "?id_data=" + getIntent().getStringExtra("order_id")
                        + "&phone_data=" + getPrefs().getString("phone", "")).
                build();
        OkHttpManager.getInstance().getOkHttpClient().newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                OkHttpManager.backgroundThreadShortToast(OrderPaiedActivity.this, "错误：" + e.toString());
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                orderDetail = new Gson().fromJson(response.body().string(), OrderDetail.class);
                new AsyncTask<Void, Void, Void>() {
                    @Override
                    protected Void doInBackground(Void... params) {
                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void aVoid) {
                        super.onPostExecute(aVoid);
                        tvMoney.setText(orderDetail.getMoney());
                        adapter.swapData(orderDetail.getArr1());
                        List<OrderDetail.Re> reList = orderDetail.getRe_arr();
                        String str = "";
                        if (reList != null && reList.size() > 0) {
                            for (OrderDetail.Re re : reList) {
                                str += re.getContent() + "\n" + re.getTime() + "\n";
                            }
                            str.substring(0, str.length() - 2);
                            tvWl.setText(str);
                        }
                        /*if (orderDetail.getAddress_arr() != null) {
                            tvName.setText("姓名：" + orderDetail.getAddress_arr().getName());
                            tvPhone.setText("电话：" + orderDetail.getAddress_arr().getPhone());
                            tvAddress.setText(orderDetail.getAddress_arr().getCity() + " "
                                    + orderDetail.getAddress_arr().getCircle());
                        }*/
                    }
                }.execute();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                onBackPressed();
                break;
        }
    }
}
